home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 19 / af019.adf / ILBM>RAW.s < prev    next >
Text File  |  1978-04-04  |  11KB  |  501 lines

  1. *********************************************************
  2. *                            *
  3. *    ILBM>RAW converter, by T.Young 1990.        *
  4. *                            *
  5. *    Usage:ILBM>RAW <Source> <Destination> [M]    *
  6. *                            *
  7. *    It is recommended that you look at the        *
  8. *    include files, before reading this source.    *
  9. *                            *
  10. *********************************************************
  11.  
  12.     incdir    Include:
  13.     include    LibraryOffsets/Dos.i
  14.     include    LibraryOffsets/Exec.i
  15.     include    Macros/LibraryOpenCloseMACROS.i
  16.     include    Macros/DosMACROS.i
  17.  
  18. *Macros
  19.  
  20. ReadMacro    MACRO                ;The DOS library must be open
  21.     move.l    FileHandle,d1
  22.     move.l    \1,d2                ;Address to read into
  23.     move.l    \2,d3                ;Length to read
  24.     Call    Read
  25.     ENDM
  26.  
  27. FreeMemory    MACRO
  28.     Active    Exec_Base        ;move.l    Exec_Base,a6
  29.     move.l    \1Address,a1
  30.     beq    \1NotAllocated        ;Only free memory if allocated
  31.     move.l    \1Size,d0
  32.     Call    FreeMem
  33. \1NotAllocated
  34.     ENDM
  35.  
  36. Error        MACRO
  37. \1Error
  38.     DisplayTextCLIH    CLIHandler,\1Text,\1TextEnd    ;Included macro
  39.     bra    CloseDeAllocate                ;Skip other errors
  40.     ENDM
  41.  
  42. *Zero pointers so that we know what has been allocated or opened
  43.  
  44.     clr.l    BodyAddress        ;This ensures that if the program
  45.     clr.l    NewBodyAddress        ;can be residented since it 
  46.     clr.l    RawAddress        ;does not rely on "dc.l 0"
  47.     clr.l    FileHandle        ;zeroing the location
  48.     clr.l    Mask
  49.  
  50. *Save length and address of parameters
  51.  
  52.     move.l    a0,FileName        ;Address of parameters
  53.     move.l    d0,d5            ;length
  54.     
  55. *Open library
  56.  
  57.     LibraryOpen    DOS_Name,Dos_Base    ;See LibraryOpenCloseMACROS.i
  58.     beq        ErrorEnd        ;Send Danger signal
  59.                         ;If we can't open DOS
  60. *Get CLI handler in order to print error messages
  61.  
  62.     GetCLIHandler    CLIHandler
  63.  
  64. *Get filenames from CLI parameters
  65.  
  66.     move.l    FileName,a0        ;Retrieve address of parameters
  67.     bsr    SkipSpaces        ;See subroutines
  68.     beq    NoParametersError    ;Give syntax
  69.     cmp.b    #"?",(a0)
  70.     beq    NoParametersError    ;Give syntax
  71.  
  72.     move.l    a0,FileName
  73.     bsr    FindSpace        ;See the subroutines
  74.     beq    ParametersError
  75.     bsr    SkipSpaces
  76.     beq    ParametersError
  77.     move.l    a0,OutFile
  78.     bsr    FindSpace
  79.     beq    EndOfParameters
  80.     bsr    SkipSpaces
  81.     beq    EndOfParameters
  82.     cmp.b    #$a,(a0)
  83.     beq    EndOfParameters
  84.     cmp.b    #"M",(a0)
  85.     beq    MaskNeeded
  86.     cmp.b    #"m",(a0)
  87.     bne    ParametersError
  88. MaskNeeded
  89.     move.l    #"MASK",Mask        ;Set flag if mask is needed
  90.     cmp.b    #" ",1(a0)
  91.     beq    EndOfParameters
  92.     cmp.b    #$a,1(a0)
  93.     bne    ParametersError
  94. EndOfParameters
  95.  
  96. *Open the file
  97.  
  98.     Active    Dos_Base
  99.     move.l    FileName,d1
  100.     move.l    #Mode_Oldfile,d2
  101.     Call    Open
  102.     move.l    d0,FileHandle
  103.     beq    SourceError
  104.  
  105. *Check IFF header
  106.  
  107.     ReadMacro    #Width,#12    ;Use as temporary storage
  108.     cmp.l        #"CAT ",Width    ;Check for concatenation
  109.     bne        NotConcat
  110.     DisplayTextCLIH    CLIHandler,ConcatText,ConcatTextEnd
  111.     ReadMacro    #Width,#12
  112. NotConcat
  113.     cmp.l        #"FORM",Width    ;Check this is IFF
  114.     bne        NotIffError    ;Error - Not IFF
  115.     cmp.l        #"ILBM",Planes    ;Check ILBM
  116.     bne        NotILBMError
  117.  
  118. *Read chunk name and act accordingly
  119.  
  120.     bclr.l    #0,d5            ;Flag for Bitmap Header found
  121. ChunkLoop
  122.     jsr    ReadLong        ;Read chunk name
  123.     tst.l    d0            ;No data was read
  124.     beq    EOFError        ;Therefore; end of file
  125.     cmp.l    #"BMHD",LongBuffer
  126.     beq    BMHD            ;BMHD must come before BODY
  127.     cmp.l    #"BODY",Longbuffer    ;As specified in ILBM
  128.     beq    BODY
  129.     jsr    ReadLong        ;Read chunk length
  130.     move.l    FileHandle,d1
  131.     move.l    LongBuffer,d2
  132.     move.l    #0,d3            ;Offset_Current is 0
  133.     Call    Seek            ;Skip to next chunk
  134.     bra    ChunkLoop
  135.  
  136. *Get compression, width, height and number of planes
  137.  
  138. BMHD
  139.     bset.l        #$0,d5        ;Set flag for BMHD found
  140.     jsr        ReadLong
  141.     ReadMacro    #Width,#12    ;Read information
  142.     jsr        ReadLong
  143.     jsr        ReadLong
  144.     bra        ChunkLoop
  145.  
  146. *Deal with Body data
  147.  
  148. BODY
  149.  
  150. ;Check BMHD was found
  151.  
  152.     btst.l    #0,d5            ;Test flag
  153.     beq    BMHDError
  154.  
  155. ;Allocate Memory to read from file
  156.     jsr    ReadLong        ;Length of BODY data
  157.     Active    Exec_Base
  158.     move.l    LongBuffer,d0
  159.     clr.l    d1
  160.     Call    AllocMem
  161.     move.l    d0,BodyAddress
  162.     beq    MemoryError
  163.     Active    Dos_Base
  164.     ReadMacro    d0,Longbuffer    ;Read into address d0 size Longbuffer
  165.     move.l    LongBuffer,BodySize    ;i.e. read BODY data
  166.  
  167. ;CloseFile
  168.  
  169.     Active    Dos_Base
  170.     move.l    FileHandle,d1
  171.     Call    Close
  172.     clr.l    FileHandle        ;Ensure close-down routine knows
  173.  
  174. ;Calculate size of decompressed data
  175.  
  176.     clr.l    d0
  177.     move.w    Width,d0
  178.     move.w    d0,d1
  179.     lsr.w    #4,d0            ;Calculate width, in bytes
  180.     and.w    #$f,d1
  181.     tst.w    d1
  182.     beq    EvenAlready
  183.     addq.w    #1,d0            ;round up to account for pad bytes
  184.     clr.w    d1
  185. EvenAlready
  186.     lsl.w    #1,d0
  187.     move.l    d0,ByteWidth
  188.     mulu    Height,d0        ;Therefore calculate size of one
  189.     move.l    d0,PlaneSize        ;bitplane, in bytes
  190.     move.b    Planes,d1
  191.     mulu    d1,d0
  192.     move.l    d0,NewBodySize        ;Therefore calculate size
  193.     move.l    d0,d1            ;of data, when decompressed
  194.     cmp.l    #"MASK",Mask
  195.     bne    NoMaskPlane        ;If a mask plane is required,
  196.     add.l    PlaneSize,d1        ;add another plane to the size
  197. NoMaskPlane
  198.     move.l    d1,RawSize
  199.  
  200. ;Uncompressed? - skip decompression
  201.  
  202.     cmp.b    #1,Compression        ;Read from BMHD
  203.     blt    Uncompressed
  204.     bgt    CompressionError    ;non standard compression
  205.  
  206.     Active    Exec_Base
  207.     clr.l    d1
  208.     move.l    NewBodySize,d0
  209.     Call    AllocMem        ;Allocate the memory for the
  210.     move.l    d0,NewBodyAddress    ;decompressed data
  211.     beq    MemoryError
  212.  
  213. *Decompress
  214.  
  215.     move.l    NewBodyAddress,a1
  216.     move.l    a1,a2
  217.     add.l    NewBodySize,a2
  218.     move.l    BodyAddress,a3
  219.  
  220. ByteLoop                ;For an explanation of IFF
  221.     move.b    (a3)+,d2        ;byte-run compression
  222.     bge    VariedBytes        ;please consult;
  223.     move.b    (a3)+,d3        ;Amiga ROM Kernel Reference Manual
  224.                     ;Includes & Autodocs
  225. ByteRunLoop
  226.     move.b    d3,(a1)+
  227.     addq.b    #1,d2
  228.     cmp.b    #1,d2
  229.     bne    ByteRunLoop
  230.     cmp.l    a1,a2
  231.     bgt    ByteLoop        ;Decompression loop over ran
  232.     bra    FinishedDecompression    ;This is not good
  233.  
  234. VariedBytes
  235.  
  236. VariedBytesLoop
  237.     move.b    (a3)+,(a1)+
  238.     subq.b    #1,d2
  239.     cmp.b    #-1,d2
  240.     bne    VariedBytesLoop
  241.     cmp.l    a1,a2
  242.     bgt    ByteLoop        ;Decompression loop over-ran
  243.                     ;This should not happen
  244. FinishedDecompression
  245.     bne    FaultyIffError
  246.  
  247. ;DeAllocate memory containing Compressed data
  248.  
  249.     move.l    BodyAddress,a1
  250.     move.l    BodySize,d0
  251.     Call    FreeMem
  252.     move.l    NewBodyAddress,BodyAddress    ;Make decompression section
  253.     move.l    NewBodySize,BodySize        ;transparent
  254.     clr.l    NewBodyAddress            ;Tell system we have
  255.                         ;deallocated memory
  256. UnCompressed
  257.  
  258. *Convert to Raw
  259.  
  260. ;Allocate memory to hold raw bitplanes
  261.  
  262.     Active    Exec_Base
  263.     move.l    RawSize,d0
  264.     clr.l    d1
  265.     Call    AllocMem
  266.     move.l    d0,RawAddress
  267.  
  268. ;Convert
  269.  
  270.     move.l    BodyAddress,a0        ;Source start address
  271.     move.l    d0,a1            ;Destination start address
  272.     sub.l    ByteWidth,d0
  273.     add.l    NewBodySize,d0        ;Destination end address
  274.     move.l    d0,a2            ;to signal when finished
  275.     move.b    #1,d0            ;Bit Plane counter
  276.     
  277. ConvertLoop
  278.     clr.l    d2            ;Again - consult Manual
  279. WidthLoop                ;concerning specifics
  280.     move.b    (a0)+,(a1,d2)        ;of ILBM specifications
  281.     addq.l    #1,d2
  282.     cmp.l    ByteWidth,d2
  283.     blt    WidthLoop
  284.     bgt    ConvertError        ;Failsafe if BODY data is faulty
  285. NoError
  286. ;Next Plane?
  287.     cmp.b    Planes,d0
  288.     beq    NoMorePlanes
  289.     addq.b    #1,d0
  290.     add.l    PlaneSize,a1
  291.     bra    ConvertLoop
  292. NoMorePlanes
  293. ;Finished?
  294.     cmp.l    a1,a2
  295.     beq    FinishedConvert
  296.     move.b    #1,d0
  297.     add.l    PlaneSize,a1
  298.     sub.l    NewBodySize,a1
  299.     add.l    ByteWidth,a1
  300.     bra    ConvertLoop
  301.  
  302. FinishedConvert
  303.  
  304. ;Add mask plane if wanted
  305.  
  306.     cmp.l    #"MASK",Mask
  307.     bne    NoMask
  308.  
  309.     clr.w    d0
  310.     clr.w    d3
  311.     move.b    Planes,d3
  312.     subq.b    #2,d3            ;Loop Planes-1 times
  313. MaskByteLoop
  314.     move.l    RawAddress,a0
  315.     move.b    (a0,d0),d1
  316.     move.w    d3,d2
  317. MaskPlanesLoop                ;Mask plane is result
  318.     add.l    PlaneSize,a0        ;of all the other planes
  319.     or.b    (a0,d0),d1        ;logically `OR'ed
  320.     dbra    d2,MaskPlanesLoop
  321.     add.l    PlaneSize,a0
  322.     move.b    d1,(a0,d0)
  323.     addq.w    #1,d0
  324.     cmp.w    PlaneSize+2,d0
  325.     bne    MaskByteLoop
  326. NoMask
  327.     
  328. ;Save RAW data
  329.  
  330.     Active    Dos_Base
  331.     move.l    OutFile,d1
  332.     move.l    #Mode_Newfile,d2    ;Attempt to create file
  333.     Call    Open
  334.     move.l    d0,FileHandle        ;Not possible
  335.     beq    DestinationError
  336.     move.l    d0,d1
  337.     move.l    RawAddress,d2
  338.     move.l    RawSize,d3
  339.     Call    Write            ;Write converted data
  340.  
  341. *CloseDown
  342.  
  343.     bra    CloseDeAllocate        ;Skip error messages
  344.  
  345. *Print text in case of an error.
  346.  
  347.     Error    NoParameters        ;See macro at the top
  348.     Error    Parameters        ;of this source
  349.     Error    Source
  350.     Error    Destination        ;Prints out text concerned
  351.     Error    NotIFF            ;and branches past the
  352.     Error    NotILBM            ;other errors
  353.     Error    EOF
  354.     Error    BMHD
  355.     Error    Compression
  356.     Error    FaultyIFF
  357.     Error    Convert
  358.     Error    Memory
  359.  
  360. CloseDeAllocate                ;Close whatever is open
  361.     Active    Dos_Base
  362.     move.l    FileHandle,d1
  363.     beq    FileNotOpen
  364.     Call    Close
  365. FileNotOpen
  366.     FreeMemory    Raw        ;See Macro
  367.     FreeMemory    Body
  368.     FreeMemory    NewBody
  369. CloseDos
  370.     LibraryClose    Dos_Base
  371.     rts
  372. ErrorEnd
  373.     move.w    #$ff,d0            ;If we cannot open DOS,
  374. RedFlashLoop                ;we cannot give error messages,
  375.     move.w    #$f00,$dff180        ;so we give a danger signal instead
  376.     dbra    d0,RedFlashLoop        ;$dff180 is the hardware register
  377.     rts                ;which holds the background colour
  378.  
  379. *Subroutines
  380.  
  381. SkipSpaces            ;Increase a0 until it no longer points
  382.     cmp.b    #" ",(a0)+    ;to a space
  383.     bne    NoMoreSpaces
  384.     subq.l    #1,d5        ;Decrease d5 each space
  385.     bne    SkipSpaces    ;Return if d5 becomes zero
  386.     rts            ;Note - Zero flag is set
  387. NoMoreSpaces
  388.     subq.l    #1,a0        ;Note - Zero flag is cleared
  389.     rts
  390.  
  391. FindSpace            ;Increase a0 until it points to a space
  392.     cmp.b    #" ",(a0)+
  393.     beq    SpaceFound
  394.     subq.l    #1,d5        ;Decrease d5 each time
  395.     bne    FindSpace    ;Return if d5 becomes zero
  396.     clr.b    -1(a0)        ;Each parameter must be null terminated
  397.     rts            ;For use as a filename
  398. SpaceFound            ;Note - Zero flag set
  399.     clr.b    -1(a0)
  400.     subq.l    #1,d5        ;Zero flag is set only if d5 becomes zero
  401.     rts            ;Otherwise it is cleared
  402.  
  403. ReadLong
  404.     move.l    FileHandle,d1    ;Read 4 bytes into Longbuffer
  405.     move.l    #LongBuffer,d2
  406.     move.l    #4,d3
  407.     Call    Read
  408.     rts
  409.  
  410. *Data
  411.  
  412. Width        dc.w    0        ;These must be in
  413. Height        dc.w    0        ;this order since
  414. LongBuffer    dc.l    0        ;they are read in
  415. Planes        dc.w    0        ;one go, 12 bytes
  416. Compression    dc.w    0        ;from width.
  417. FileName    dc.l    0
  418. OutFile        dc.l    0
  419. BodyAddress    dc.l    0
  420. NewBodyAddress    dc.l    0
  421. RawAddress    dc.l    0
  422. NewBodySize    dc.l    0
  423. BodySize    dc.l    0
  424. RawSize        dc.l    0
  425. PlaneSize    dc.l    0
  426. ByteWidth    dc.l    0
  427. Mask        dc.l    0
  428. FileHandle    dc.l    0
  429. CLIHandler    dc.l    0
  430.     Dos_Library_Data
  431.  
  432. *Text
  433.  
  434. ConcatText
  435.     dc.b    "File is an IFF concatenation.",10,13
  436.     dc.b    "Assuming first FORM is required.",10,13
  437. ConcatTextEnd
  438.     even
  439.  
  440. *Error Messages
  441.  
  442. FaultyIFFText
  443.     dc.b    "Error - Faulty IFF compression, decompression loops overrun.",10,13
  444. FaultyIFFTextEnd
  445.     even
  446.  
  447. ConvertText
  448.     dc.b    "Error - Faulty IFF interleave, conversion loop overrun.",10,13
  449. ConvertTextEnd
  450.  
  451. NoParametersText
  452.     dc.b    "ILBM to RAW converter, Copyright T.Young 1990.",10,13
  453.     dc.b    "Usage: ILBM>RAW <Source> <Destination> [M]",10,13
  454. NoParametersTextEnd
  455.     
  456. ParametersText
  457.     dc.b    "Error - mistake in parameters.",10,13
  458.     dc.b    "Usage: ILBM>RAW <Source> <Destination> [M]",10,13
  459. ParametersTextEnd
  460.     even
  461.  
  462. SourceText
  463.     dc.b    "Error - Cannot open source file.",10,13
  464. SourceTextEnd
  465.     even
  466.  
  467. DestinationText
  468.     dc.b    "Error - Cannot open destination file.",10,13
  469. DestinationTextEnd
  470.     even
  471.  
  472. MemoryText
  473.     dc.b    "Error - Out Of Memory.",10,13
  474. MemoryTextEnd
  475.     even
  476.  
  477. NotIFFText
  478.     dc.b    "Error - Source file not IFF.",10,13
  479. NotIFFTextEnd
  480.     even
  481.  
  482. NotILBMText
  483.     dc.b    "Error - Source file not ILBM.",10,13
  484. NotILBMTextEnd
  485.     even
  486.  
  487. EOFText
  488.     dc.b    "Error - No IFF BODY chunk.",10,13
  489. EOFTextEnd
  490.     even
  491.  
  492. BMHDText
  493.     dc.b    "Error - No IFF bitmap header before BODY.",10,13
  494. BMHDTextEnd
  495.     even
  496.     
  497. CompressionText
  498.     dc.b    "Error - Not IFF standard compression.",10,13
  499. CompressionTextEnd
  500.     even
  501.